import { mesDefectiveReviewComment, commitReview, getReviewResultList } from "@/api/review/mesDefectiveReview"
import { getDict } from "@/api/common"

export const form = {
  data() {
    return {
      isLoadingsub: false,
      formDataDialogType: [],
      formDataDialog1: {},
      visible1: false,
      // 新增/删除表单配置
      formDescDialog1: {
        span: 24,
        disabled: false,
        isShowSubmitBtn: true,
        labelPosition: "top",
        formDesc: {
          processCode: {
            type: "number",
            label: $i18n.t("mesDefectiveReview.planCode"),
            layout: 12,
            // required: true,
            disabled: true
          },
          orderCode: {
            type: "input",
            label: $i18n.t("mesDefectiveReview.orderCode"),
            layout: 12,
            disabled: true
          },
          processCode: {
            type: "input",
            label: $i18n.t("common.processCode"),
            layout: 12,
            required: false,
            disabled: true
          },
          processName: {
            type: "input",
            label: $i18n.t("common.processName"),
            layout: 12,
            required: false,
            disabled: true
          },
          equipmentCode: {
            type: "input",
            label: $i18n.t("mesDefectiveReview.equipmentCode"),
            layout: 12,
            required: false,
            disabled: true
          },
          equipmentName: {
            type: "input",
            label: $i18n.t("mesDefectiveReview.equipmentName"),
            layout: 12,
            required: false,
            disabled: true
          },
          seriesCode: {
            type: "input",
            label: $i18n.t("mesDefectiveReview.seriesCode"),
            layout: 12,
            required: false,
            disabled: true
          },
          seriesName: {
            type: "input",
            label: $i18n.t("mesDefectiveReview.seriesName"),
            layout: 12,
            required: false,
            disabled: true
          },
          faultCode: {
            type: "input",
            label: $i18n.t("mesDefectiveReview.faultCode"),
            layout: 12,
            required: false,
            disabled: true
          },
          faultName: {
            type: "input",
            label: $i18n.t("mesDefectiveReview.faultName"),
            layout: 12,
            required: false,
            disabled: true
          },
          rejectQuantity: {
            type: "number",
            label: $i18n.t("mesDefectiveReview.rejectQuantity"),
            layout: 12,
            disabled: true
          },
          rejectRatio: {
            type: "number",
            label: $i18n.t("mesDefectiveReview.rejectRatio"),
            layout: 12,
            disabled: false,
            required: true,
            disabled: true,
            attrs: {
              min: 0
            }
          },
          description: {
            type: "textarea",
            label: $i18n.t("mesDefectiveReview.description"),
            layout: 24,
            required: true,
            disabled: true
          },
          table: { layout: 24, type: "input", label: $i18n.t("mesDefectiveReview.description") },

          defectLevel: {
            type: "radio",
            label: $i18n.t("mesDefectiveReview.defectLevel"),
            layout: 24,
            required: true,
            prop: {
              text: "description",
              value: "value"
            },
            // Promise 对象
            options: async (data) => {
              const list = await getDict("defect_level")
              return list.data.result
            }
          },

          reviewResult: {
            type: "radio",
            label: $i18n.t("mesDefectiveReview.reviewResult"),
            layout: 24,
            required: true,
            //0-让步接收、1-返工返修、2-报废
            // Promise 对象
            prop: {
              text: "description",
              value: "value"
            },
            options: async (data) => {
              const list = await getReviewResultList()
              return list.data.result
            }
          },
          reviewOpinion: {
            type: "textarea",
            label: $i18n.t("mesDefectiveReview.auditOpinion"),
            layout: 24,
            required: true
          }
        }
      }
    }
  },
  methods: {
    async handleSubmit1(data) {
      if (this.isLoadingsub) return // 判断状态
      // const params = this.formDataDialog.id
      //   ? { title: "修改成功", api: rowUpdate, data }
      //   : { title: "添加成功", api: rowCreate, data };

      try {
        // let { reviewCode } = this.formDataDialog1;
        // let data = {
        //   ...row,
        //   reviewCode,
        // };
        this.isLoadingsub = true
        await commitReview(data)
        this.$message.success($i18n.t("mesDefectiveReview.finalReviewSuccess"))

        this.visible1 = false
        this.refresh()
      } finally {
        this.isLoadingsub = false
        this.visible1 = false
      }
    },

    //提交部门的
    async sumInput(row) {
      let { reviewCode } = this.formDataDialog1
      let data = {
        ...row,
        reviewCode
      }
      if (!row.commentOpinion) {
        this.$message.warning(`${row.commentName} ${$i18n.t("mesDefectiveReview.auditOpinionRequired")}`)
        return
      }

      try {
        this.isLoadingsub = true
        await mesDefectiveReviewComment(data)
        this.$message.success(`${row.commentName}${$i18n.t("mesDefectiveReview.submitSuccess")}`)

        this.visible1 = false
        this.refresh()
      } finally {
        this.isLoadingsub = false
        this.visible1 = false
      }
    }
  }
}
